<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input
        clearable
        placeholder="请输入内容"
        class="search-main"
        v-model="params.name"
        @clear="search"
      />
      <el-button type="primary" @click="search">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="addEmployee">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" prop="phonenumber" />
        <el-table-column label="角色" prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="200">
          <template #default="scope">
            <el-button size="mini" type="text">编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="delEnterprise(scope.row.id)"
              >删除</el-button
            >
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="page-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[5, 10, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 添加员工 -->
    <el-dialog
      title="添加员工"
      width="480px"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form
          ref="addForm"
          :model="addForm"
          :rules="addFormRules"
          label-width="80px"
        >
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getEmployeeListAPI,
  getAllRoleAPI,
  addEmployeeListAPI,
  delEmployeeAPI
} from '@/api/employee'
export default {
  name: 'Employee',
  data () {
    return {
      // 请求参数
      params: {
        name: '', // 姓名
        page: 1, // 页数
        pageSize: 5 // 页容量
      },
      employeeList: [], // 员工列表
      total: 0, // 总页数
      dialogVisible: false, // 控制弹框打开关闭
      roleList: [], // 角色列表
      addForm: {
        name: '', // 员工姓名
        phonenumber: '', // 联系方式
        roleId: '', // 用户角色id
        status: 1, // 停用状态
        userName: '' // 登录账号
      },
      addFormRules: {
        name: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [{ required: true, message: '请分配角色', trigger: 'blur' }],
        status: [{ required: true, message: '请选择员工状态', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getEmployeeList() // 获取员工列表
  },
  methods: {
    // 获取员工列表
    async getEmployeeList () {
      const res = await getEmployeeListAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
      // console.log('员工列表',res.data.rows)
    },
    // 页容量改变时触发
    handleSizeChange (val) {
      this.params.pageSize = val
      this.getEmployeeList()
    },
    // 页码改变时触发
    handleCurrentChange (val) {
      this.params.page = val
      this.getEmployeeList()
    },
    // 处理序号
    indexMethod (index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 查询
    search () {
      this.params.page = 1
      this.getEmployeeList()
    },
    // 添加员工
    async addEmployee () {
      this.dialogVisible = true
      const res = await getAllRoleAPI()
      console.log('获取角色列表', res.data)
      this.roleList = res.data
    },
    // 关闭弹窗
    closeDialog () {
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
    },
    // 添加员工 提交
    submit () {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          await addEmployeeListAPI(this.addForm)
          this.$message.success('添加成功')
          this.getEmployeeList()
          this.closeDialog()
        }
      })
    },
    // 格式化状态
    formatStatus (status) {
      const statusMap = {
        0: '未启用',
        1: '启用'
      }
      return statusMap[status]
    },
    // 删除员工
    delEnterprise (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async () => {
        await delEmployeeAPI(id)
        this.$message.success('删除成功')
        this.getEmployeeList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
